
package com.bh.framework.network;

import com.bh.framework.common.LoadingDialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;

public abstract class NetAsyncTask<Params, Progress, Result> extends
		AsyncTask<Params, Progress, Result>
{
	private LoadingDialog ld;
	protected Context taskContext;
	private boolean isShow = true;
	private boolean isCancel = true;

	public boolean isShow()
	{
		return isShow;
	}

	public void setShow(boolean isShow)
	{
		this.isShow = isShow;
	}
	
	public void setCancel(boolean isCancel){
		this.isCancel = isCancel;
	}

	public NetAsyncTask(Context activity,
			final DialogInterface.OnCancelListener l,
			final boolean interruptTask, final boolean interruptIfRunning,
			boolean isShow)
	{
		super();
		this.isShow = isShow;
		if (!(taskContext != null && taskContext.equals(activity) && ld != null) && isShow)
		{
			ld = new LoadingDialog(((Activity)activity), "正在加载...");
			ld.setOnCancelListener(new DialogInterface.OnCancelListener()
			{
				@Override
				public void onCancel(DialogInterface dialog)
				{
					if (interruptTask)
					{
						NetAsyncTask.this.cancel(interruptIfRunning);
					}
					if (l != null)
					{
						l.onCancel(dialog);
					}
				}
			});
		}
		taskContext = activity;
	}

	@Override
	protected Result doInBackground(Params... params)
	{
		return null;
	}

	@Override
	protected void onPostExecute(Result result)
	{
		super.onPostExecute(result);
		if (ld != null && isCancel)
		{
			ld.close();
		}

	}

	@Override
	protected void onPreExecute()
	{
		super.onPreExecute();
		if (ld != null && isShow && !ld.isShow())
		{
			ld.show();
		}
	}

}
